/**
 * aui-popup.js
 * @author 流浪男
 * @todo more things to abstract, e.g. Loading css etc.
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 */
(function(window, undefined) {
    "use strict";
    var auiToast = function() {
        // this.create();
    };
    var isShow = false;
    auiToast.prototype = {
        create: function(params, callback) {
            var self = this;
            var toastHtml = '';
            var titleHtml = params.title ? '<div class="aui-toast-content">' + params.title + '</div>' : '';
            switch (params.type) {
                case "success":
                    toastHtml = '<div class="aui-toast aui-success"><span class="iconfont icon-dui"></span>' + titleHtml + '</div>';
                    break;
                case "fail":
                    toastHtml = '<div class="aui-toast aui-fail"><span class="iconfont icon-cuowu"></span>' + titleHtml + '</div>';
                    break;
                case "warning":
                    toastHtml = '<div class="aui-toast aui-warning"><span class="iconfont icon-bg-warning"></span>' + titleHtml + '</div>';
                    break;
            }
            if (document.querySelector(".aui-toast")) return;
            document.getElementById('app').insertAdjacentHTML('beforeend', toastHtml);
            var duration = params.duration ? params.duration : "2000";
            self.show();
            if (params.type == 'loading') {
                if (callback) {
                    callback({
                        status: "success"
                    });
                };
            } else {
                setTimeout(function() {
                    self.hide();
                }, duration)
            }
        },
        show: function() {
            var self = this;
            document.querySelector(".aui-toast").style.display = "block";
            document.querySelector(".aui-toast").style.marginTop = "-" + Math.round(document.querySelector(".aui-toast").offsetHeight / 2) + "px";
            if (document.querySelector(".aui-toast")) return;
        },
        hide: function() {
            var self = this;
            if (document.querySelector(".aui-toast")) {
                document.querySelector(".aui-toast").parentNode.removeChild(document.querySelector(".aui-toast"));
            }
        },
        remove: function() {
            if (document.querySelector(".aui-dialog")) document.querySelector(".aui-dialog").parentNode.removeChild(document.querySelector(".aui-dialog"));
            if (document.querySelector(".aui-mask")) {
                document.querySelector(".aui-mask").classList.remove("aui-mask-out");
            }
            return true;
        },
        success: function(params, callback) {
            var self = this;
            params.type = "success";
            return self.create(params, callback);
        },
        fail: function(params, callback) {
            var self = this;
            params.type = "fail";
            return self.create(params, callback);
        },
        warning: function(params, callback) {
            var self = this;
            params.type = "warning";
            return self.create(params, callback);
        },
        loading: function(params, callback) {
            var self = this;
            params.type = "loading";
            return self.create(params, callback);
        }
    };
    window.auiToast = auiToast;
})(window);